package
{
	import meke meu putz
	
	import flash.events.Event;
	import flash.display.MovieClip;
	/**
	 * @author perry
	 */
	public class ECGReader extends MovieClip
	{
		private var SCALE:Number 			= 100;
		private var colorLine:Number		= 0xFFFFFF;
		private var strokeLine:Number		= 0.25;
		
//		private var canvas:Sprite;
		private var path:String;
		
		private var ecgDataLoader:ECGDataLoader;
		private var plotData:Vector.<Object>;
		
		private var pos:int = 0;
		private var totalTime:Number;
		private var halfStage:Number;
		
		public function ECGReader()
		{
			trace("ECGReader - bruno perry - 2010")
		}
		
		public function init():void
		{
			buildECGReader();
		}
		
		public function loadFile( pth:String ):void
		{
			path = pth;
			
			trace("path")
//			ecgDataLoader.init( path );
		}

		private function buildECGReader():void
		{
			
//			canvas = new Sprite();
			canvas.y = this.stage.stageHeight / 2;
//			addChild( canvas );
			
			halfStage = this.stage.stageWidth / 2;
			
//			ecgDataLoader = new ECGDataLoader();
//			ecgDataLoader.addEventListener( ECGDataLoader.COMPLETE, ecgDataCompleteHandler );
		}
		
		private function setLabels():void
		{
			if( plotData )
			{
//				secLabel.text = String( plotData[plotData.length - 2].t );
			}
		}
		
		private function paint():void
		{
			canvas.graphics.clear();
			canvas.graphics.lineStyle(strokeLine, colorLine);
			canvas.graphics.moveTo( plotData[0].t, plotData[0].mV );
			
			this.addEventListener(Event.ENTER_FRAME, loop );
		}

		private function loop( e:Event ):void
		{
			canvas.graphics.lineTo(plotData[pos].t * SCALE, plotData[pos].mV * SCALE);
			
			if( pos + 1 >= totalTime )
			{
				this.removeEventListener(Event.ENTER_FRAME, loop );
			}
			
			canvas.x = halfStage - canvas.width;
			
			pos++;
		}


		private function ecgDataCompleteHandler( e:Event ):void
		{
			plotData = ecgDataLoader.data;
			
			totalTime = plotData.length;
			
			setLabels();
			
			paint();
		}

	}
}
